package com.zhonglz.service.impl;


import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.security.PublicKey;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.zhonglz.commons.base.BaseService;
import com.zhonglz.model.BuCertInfo;
import com.zhonglz.service.BuCertInfoService;
import com.zhonglz.system.domian.SetCertMain;
import com.zhonglz.util.cert.CertUtils;
import com.zhonglz.util.date.DateUtil;
import com.zhonglz.util.result.ResultUtils;
import com.zhonglz.util.tools.UuidUtils;

/***
 * 证书信息接口实现
 * @author admin
 *
 */
@Service
@Transactional 
public class BuCertInfoServiceImpl extends BaseService implements BuCertInfoService{
	
	private static Logger logger = LoggerFactory.getLogger(BuCertInfoServiceImpl.class);

	
	/****
	 * 分页查询出证书信息
	 */
	@Override
	public List<BuCertInfo> selectByDataList(Map<String, Object> mapParam) {
		
		return buCertInfoMapper.selectByDataList(mapParam) ;
	}
	
	/***
	 * 生成软证书
	 * 支持sm2、rsa
	 */
	@Override
	public ResultUtils setCert(SetCertMain setCertMain) {
		String certId = UuidUtils.uuid();//证书uuid
		String fileUid = UuidUtils.uuid();//文件uuid
		BuCertInfo certInfo = new BuCertInfo();
		certInfo.setCertId(certId);//证书uuid
		certInfo.setCertUrl(fileUid);//文件uuid
		certInfo.setCertPwd(setCertMain.getPassWord());//证书默认密码  实际应用时候，这里做对称加密处理，或者摘要加盐值
		certInfo.setCertC(setCertMain.getCountry());//C项 国家
		certInfo.setCertSt(setCertMain.getProvince());//ST项 省份
		certInfo.setCertL(setCertMain.getLocation());//L项 城市
		certInfo.setCertO(setCertMain.getBaseInfoO());//O项 公用信息
		certInfo.setCertCn(setCertMain.getCommonName());//CN项 公用名
		certInfo.setCertOu(setCertMain.getOrganization());//OU项 部门/二级机构
		certInfo.setCertType(setCertMain.getAlgorithmType());//证书类型
		certInfo.setCertValidTime(Integer.valueOf(setCertMain.getCertExpire()));//证书有效期
		certInfo.setCertNotBefore(new Date());//证书生成时间
		certInfo.setCertNotAfter(DateUtil.dateByString(DateUtil.getLastDate(certInfo.getCertValidTime())));//证书失效时间
		certInfo.setCreateTime(new Date());//创建时间
		
		
		ResultUtils rs=new ResultUtils();
		if("sm2".equals(setCertMain.getAlgorithmType())) {
			//sm2软证书生成
			  Map<String, byte[]> result = CertUtils.createSM2CertToOne(certInfo.getCertC(),certInfo.getCertCn(),certInfo.getCertOu(),certInfo.getCertO(),certInfo.getCertSt(),certInfo.getCertL(),certInfo.getCertValidTime(),certInfo.getCertPwd());
			  byte[] certSYBase64 =result.get("certData");//证书数据
			  InputStream is = new ByteArrayInputStream(certSYBase64);
			  byte[] certGYBase64 =result.get("publicKey");//证书公钥
			  certInfo.setCertPublicKey(certGYBase64);
			  rs=sysFileInfoService.uploadFile(fileUid,certId+".pfx", ".pfx", is);
		}else if("rsa".equals(setCertMain.getAlgorithmType())) {
			 Map<String, byte[]> result= CertUtils.createRSACertToOne(certInfo.getCertC(),certInfo.getCertCn(),certInfo.getCertOu(),certInfo.getCertO(),certInfo.getCertSt(),certInfo.getCertL(),certInfo.getCertValidTime(),certInfo.getCertPwd());
			 byte[] certSYBase64 =result.get("keyStoreData");//证书数据
			 InputStream is = new ByteArrayInputStream(certSYBase64);
			 
			 byte[] certGYBase64 =result.get("publicKey");//证书公钥     certificateData表示公钥证书文件流下载，publicKey 数据库存储公钥byte数组
			 certInfo.setCertPublicKey(certGYBase64);
			 rs=sysFileInfoService.uploadFile(fileUid,certId+".p12", ".p12", is);
		}
		
		//添加证书入库
		int count = buCertInfoMapper.insertSelective(certInfo);
		if(count<1) {
			return new ResultUtils(ResultUtils.CODE_ERROR,"证书信息入库失败");
		}
		return rs;
	}

	
	/***
	 * 证书签名
	 * @param algorithmType 证书算法 sm2 rsa
	 * @param certFile 证书文件
	 * @param originalText 签名文本
	 * @param passWord 证书密码
	 * @return
	 * @throws Exception
	 */
	@Override
	public ResultUtils signSm2Cert(MultipartFile certFile, String originalText, String passWord) {
			return CertUtils.signSM2Cert(certFile, originalText, passWord);
	}

	/****
	 * 验签
	 * @param publicKey 公钥
	 * @param originalText 签名原文
	 * @param signData 签名值
	 * @return
	 */
	@Override
	public ResultUtils verifySM2Cert(String publicKey, String originalText, String signData) {
		return CertUtils.verifySM2Cert(publicKey, originalText, signData);		
	}

	/***
	 * rsa验签
	 * @param certFile	证书文件
	 * @param passWord	证书密码
	 * @param originalText 签名原文
	 * @param signData 签名值
	 * @return
	 */
	@Override
	public ResultUtils verifyRSACert(MultipartFile certFile, String passWord, String originalText, String signData) {
		return CertUtils.verifyRSACert(certFile, passWord,originalText, signData);
	}

	@Override
	public BuCertInfo selectById(String id) {
		return buCertInfoMapper.selectByPrimaryKey(id);
	}


	

	

}
